//
//  LoginController.swift
//  edu
//
//  Created by 周富荣 on 15/12/23.
//  Copyright © 2015年 karlzhou. All rights reserved.
//

import UIKit

class LoginController: UIViewController,XMPPStreamDelegate {
    @IBOutlet var loginTf: UITextField!
    
    @IBOutlet var loginBt: UIButton!
    @IBOutlet var passwordTf: UITextField!
    @IBOutlet var registerBt: UIButton!
    var xmppStream:XMPPStream?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func chageDevie(notification:NSNotification){
        
        
    }
    
    @IBAction func login(sender: AnyObject) {
        let username=self.loginTf.text!;
        let password=self.passwordTf.text!
        if(password.isEmpty || username.isEmpty){
            print("can not be null")
        }else{
            XMPPManager.shareManager().login(username, password: password,delegate: self)
        }
    }
    @IBAction func register(sender: AnyObject) {
        
    }
    
    func xmppStreamDidConnect(sender: XMPPStream!) {
        do{
            try sender.authenticateWithPassword(self.passwordTf.text)
        }catch{
            
        }
    }
    
    func xmppStreamDidAuthenticate(sender: XMPPStream!) {
        print("login success")
      
        let mainStoryboard =  UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let mainVC=mainStoryboard.instantiateInitialViewController();
        //let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
       // delegate.window?.rootViewController=mainVC
        self.presentViewController(mainVC!, animated: true, completion:{
            let presence = XMPPPresence()
            sender.sendElement(presence)
            
            })
       
    }
    
    
    
}
